﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.DataManagement;
using KOPLibrary.Forms;

namespace KOPLibrary.Controls
{
    public partial class DataBindControl : UserControl
    {
        public DataBindControl()
        {
            InitializeComponent();
            ctlMsgBody.MaxLength = int.MaxValue;
        }

        private AbstractData dataSource;
        private BarcodeParameters barcodeSettings = BarcodeParameters.Empty;

        public bool BarcodeEnabled
        {
            get
            {
                return ctlBarcodePanel.Visible;
            }
            set
            {
                ctlBarcodePanel.Visible = value;
            }
        }

        public BarcodeParameters UsedBarcodeSettings
        {
            get
            {
                if (ctlBarcode.IsEmpty())
                    return null;
                else return barcodeSettings;
            }
        }

        public string BarcodeField
        {
            get
            {
                return ctlBarcode.SelectedColumn;
            }
        }

        public AbstractData DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
                ctlDataCol.DataSource = value;
                ctlBarcode.DataSource = value;
            }
        }

        private void ctlAddField_Click(object sender, EventArgs e)
        {
            ctlMsgBody.Select();
            ctlMsgBody.SelectedText = "{#" + ctlDataCol.SelectedColumn + " #}";
        }

        public string BoundText
        {
            get
            {
                return ctlMsgBody.Text;
            }

            set
            {
                ctlMsgBody.Text = value;
            }
        }

        private void columnList1_ChangeColumn(object sender, EventArgs e)
        {
            ctlBarcodeSettings.Visible = !ctlBarcode.IsEmpty();
        }

        private void ctlBarcodeSettings_Click(object sender, EventArgs e)
        {
            BarcodeSettingsForm form = new BarcodeSettingsForm();
            form.Settings = this.barcodeSettings;
            if (form.Execute())
                barcodeSettings = form.Settings;
        }

        private void ctlMsgBody_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
